package com.my.algorithmlearning.leetcode;

import org.springframework.util.Assert;

/**
 * @ClassName Medium_96_CountDifferentSearchTrees
 * @Description 题解：https://leetcode.cn/problems/unique-binary-search-trees/solution/hua-jie-suan-fa-96-bu-tong-de-er-cha-sou-suo-shu-b/
 * @Author wanghaiwei
 * @Date 2023/3/29 13:32
 */
public class Medium_96_CountDifferentSearchTrees {
    public int numTrees(int n) {
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        int[] dp = new int[n + 1];
        //注意：dp0为1
        dp[0] = 1;
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                dp[i] += dp[j - 1] * dp[i - j];
            }
        }
        return dp[n];
    }

    public static void main(String[] args) {
        Medium_96_CountDifferentSearchTrees solution = new Medium_96_CountDifferentSearchTrees();
        Assert.isTrue(solution.numTrees(4) == 14, "n = 4计算错误，预期14");
    }
}
